package com.moorror.codegenvel.common.table;

import com.google.common.collect.Maps;
import org.apache.commons.lang3.reflect.FieldUtils;

import java.util.Map;

/**
 * @Author jiyanghuang
 * @Date 2022/9/16 21:46
 */
public enum ColumnValueEnum {

    INT("int"),
    INTEGER("java.lang.Integer"),
    BOOLEAN("boolean"),
    BOOLEAN_C("java.lang.Boolean"),
    DOUBLE("double"),
    DOUBLE_C("java.lang.Double"),
    BIG_DECIMAL("java.math.BigDecimal"),
    STRING("java.lang.String"),
    PRIMARY("PRIMARY"),
    JSON("JSON"),
    SELECT("SELECT"),
    CHECKBOX("CHECKBOX"),
    ESCAPE_JSON("ESCAPE_JSON"),
    SELECT_MULTI("SELECT_MULTI"),
    ;

    private static Map<String, ColumnValueEnum> columnValueEnumMap = Maps.newHashMap();

    static {
        ColumnValueEnum[] columnValueEnums = ColumnValueEnum.values();
        for (ColumnValueEnum columnValueEnum : columnValueEnums) {
            columnValueEnumMap.put(columnValueEnum.getColumnType(), columnValueEnum);
        }
    }

    private String columnType;

    ColumnValueEnum(String columnType) {
        this.columnType = columnType;
    }

    public String getColumnType() {
        return columnType;
    }

    public String transferValue(Object value) {
        if (this == INT) {
            return String.valueOf(value);
        } else if (this == INTEGER) {
            return String.valueOf(value);
        } else if (this == BOOLEAN) {
            return String.valueOf(value);
        } else if (this == BOOLEAN_C) {
            return String.valueOf(value);
        } else if (this == DOUBLE) {
            return String.valueOf(value);
        } else if (this == DOUBLE_C) {
            return String.valueOf(value);
        } else if (this == BIG_DECIMAL) {
            return String.valueOf(value);
        } else if (this == STRING) {
            return (String) value;
        } else if (this == JSON) {
            return String.valueOf(value);
        } else if (this == ESCAPE_JSON) {
            return String.valueOf(value);
        }
        return String.valueOf(value);
    }

    public static ColumnValueEnum fetchByColumnType(String columnType) {
        return columnValueEnumMap.get(columnType);
    }

    public void writeFieldValue(Object obj, String fieldName, Object value) {
        try {
            if (this == INT) {
                FieldUtils.writeDeclaredField(obj, fieldName, value, true);
            } else if (this == INTEGER) {
                FieldUtils.writeDeclaredField(obj, fieldName, value, true);
            } else if (this == BOOLEAN) {
                FieldUtils.writeDeclaredField(obj, fieldName, value, true);
            } else if (this == BOOLEAN_C) {
                FieldUtils.writeDeclaredField(obj, fieldName, value, true);
            } else if (this == DOUBLE) {
                FieldUtils.writeDeclaredField(obj, fieldName, value, true);
            } else if (this == DOUBLE_C) {
                FieldUtils.writeDeclaredField(obj, fieldName, value, true);
            } else if (this == BIG_DECIMAL) {
                FieldUtils.writeDeclaredField(obj, fieldName, value, true);
            } else if (this == STRING) {
                FieldUtils.writeDeclaredField(obj, fieldName, value, true);
            } else if (this == JSON) {
                FieldUtils.writeDeclaredField(obj, fieldName, value, true);
            } else if (this == ESCAPE_JSON) {
                FieldUtils.writeDeclaredField(obj, fieldName, value, true);
            } else {
                FieldUtils.writeDeclaredField(obj, fieldName, value, true);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
